<template>
  <div class="flexContent">
    <!-- 系统设置 -->
    <div class="tokenTitle">订单起送价格</div>
    <div class="formBox">
      <span style="color: rgb(226, 12, 12);">*</span>订单起送价格 <el-input v-clearEmoji v-model.trim="minPrice.val"
        style="width: 180px;margin-right: 100px;" />
      <el-button class="submitBtn" @click="minPriceSubmit">提交</el-button>
    </div>
    <div class="tokenTitle">客服经理电话</div>
    <div class="formBox">
      <span style="color: rgb(226, 12, 12);">*</span>客服经理姓名 <el-input v-clearEmoji v-model.trim="service.name"
        style="width: 230px;margin-right: 20px;" />
      <span style="color: rgb(226, 12, 12);">*</span>电话 <el-input v-clearEmoji v-model.trim="service.val"
        style="width: 230px;margin-right: 100px;" />
      <el-button class=" submitBtn" @click="servicePhoneBtn">提交</el-button>
    </div>
    <!-- <div class="formBox">
      <el-input v-clearEmoji v-model.trim="name" style="width: 230px;margin-right: 100px;" />

    </div> -->
  </div>
</template>

<script>
import { getShopConfig, updateShopConfig, updateShopConfigPhone } from '@/api/shopManagement'
export default {
  name: '',
  data() {
    return {
      name: '',
      minPrice: {
        id: '',
        val: ''
      },
      service: {
        id: '',
        name: '',
        val: ''
      }
    }
  },
  created() {
    this.getShopConfig()
  },
  mounted() { },
  methods: {
    async getShopConfig() { // 获取店铺配置
      const res = await getShopConfig()
      if (res.code === 200) {
        console.log('res', res);
        this.minPrice.val = res.data.minPrice.value
        this.minPrice.id = res.data.minPrice.id
        this.service.id = res.data.service.id
        this.service.name = res.data.service.name
        this.service.val = res.data.service.value

      }
    },
    async minPriceSubmit() {
      if (this.minPrice.val === '') {
        this.$message.error('请输入起送价格')
        return
      }
      // const reg = /^[0-9]+$/
      // if (!reg.test(this.minPrice.value)) {
      //   this.$message.error('请输入数字')
      //   return
      // }
      if (this.minPrice.val < 0) {
        this.$message.error('请输入大于0的数字')
        return
      }
      const res = await updateShopConfig({ value: this.minPrice.val, id: this.minPrice.id })
      if (res.code === 200) {
        this.$message.success('修改成功')
        this.getShopConfig()
      }
    },
    async servicePhoneBtn() {
      if (this.service.name === '' || this.service.val === '') {
        this.$message.error('电话和姓名不能为空')
        return
      }
      const reg = /^(?:[\u4e00-\u9fa5·]{2,16})$/
      if (!reg.test(this.service.name)) {
        this.$message.error('请输入中文姓名')
        return
      }
      const reg2 = /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1589]))\d{8}$/
      if (!reg2.test(this.service.val)) {
        this.$message.error('请输入正确的手机号')
        return
      }
      const res = await updateShopConfigPhone({ value: this.service.val, name: this.service.name, id: this.service.id })
      if (res.code === 200) {
        this.$message.success('修改成功')
        this.getShopConfig()
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.flexContent {
  .tokenTitle {
    font-size: 18px;
    font-weight: bold;
    width: 150px;
    text-decoration: underline;
    margin-bottom: 10px;
  }

  .formBox {
    margin-bottom: 30px;
  }
}
</style>
